#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#include "listas.h"

void addToList(gl *list, void *data)
{
    if (list->data == NULL)
    {
        list->data = data;
        list->sgt = NULL;
    }
    else
    {
        gl *aux = list;
        while (aux->sgt!=NULL)
        {
            aux = aux->sgt;
        }
        gl *newNode = (gl*) malloc(sizeof(gl));
        newNode->data = data;
        newNode->sgt = NULL;
        aux->sgt = newNode;
    }
}

void addListToList(gl *list, gl *listToAdd)
{
    int i = 0;
    gl *q = listToAdd;
    for(i=0;q!=NULL;i++)
    {
      addToList(list,q->data);
      q = q->sgt;
    }
}

void deleteOfList(gl *list, void *data)
{
    gl *aux = list;
    if (aux!=NULL)
    {
        if (aux->data!=data)
        {
            while (aux->sgt!=NULL && aux->sgt->data!=data)
            {
                aux = aux->sgt;
            }
            if (aux->sgt != NULL)
            {
                gl *dispose;
                dispose = aux->sgt;
                aux->sgt = aux->sgt->sgt;
                free(dispose);
            }
        }
        else
            *list = *aux->sgt;
    }
}

void deleteList(gl *list, gl *listToDelete)
{
    int i = 0;
    gl *q = listToDelete;
    for(i=0;q!=NULL;i++)
    {
      gl *aux = q->sgt;
      deleteOfList(list,q->data);
      q = aux;
    }
}

void deleteOfListByPosition(gl *list, int pos)
{
    gl *aux = list;
    gl *ant = list;
    int i = 0;
    if (aux!=NULL)
    {
        for (i = 0;aux != NULL && i < pos; i++)
        {
            aux = aux->sgt;
            if (i!=0)
                ant = ant->sgt;
        }
        if (i!=0)
        {
            if (aux!=NULL)
            {
                gl *dispose;
                dispose = aux;
                ant->sgt = aux->sgt;
                free(dispose);
            }
        }
        else
            *list = *aux->sgt;

    }
}


void insertarOrdenado(gl *list,void *data){

   gl*  ptrAnterior;
   gl*  ptrActual;

   gl *ptrNuevo = (gl*) malloc(sizeof(gl));
   ptrNuevo->data = data;
   ptrNuevo->sgt = NULL;
   
   ptrAnterior=NULL;
   ptrActual=list;
    
   while((ptrActual!=NULL)&&(data > ptrActual->data)){
      
      ptrAnterior=ptrActual;
      ptrActual=ptrActual->sgt;
      
   } /*Fin de while*/
    
   if (ptrAnterior==NULL){                                        /*Inserta elemento al ppio de la lista*/
      ptrNuevo->sgt=list;
      list=ptrNuevo;
      
   }/*Fin de if*/
   else{
      ptrAnterior->sgt=ptrNuevo;
      ptrNuevo->sgt=ptrActual;
   }/*Fin de else*/   
              

}/*Fin insertarOrdenado*/


int buscar(gl *ptrActual,void *data){

   int i=0;

   while((ptrActual!=NULL)&&(ptrActual->data!=data)){
      ptrActual=ptrActual->sgt;  
      i++;
   }/*Fin while*/
   
   if(ptrActual==NULL){
      i=-1;
   }
   
   return i; 

}

int longitud(gl  *ptrActual) {
	
	int n=0;
	
	while (ptrActual != NULL) {  
	    ++n;
	   ptrActual = ptrActual->sgt;
	}
	return n;

}






